//
//  HHNineViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/6.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHSixViewController.h"
#import "HHScrollSegmentView.h"
#import "HHContentView.h"
#import "HHViewController.h"
#import "MJRefresh.h"
#import "HHTabController.h"
#import "HHCollectionViewController.h"
NSString *const HHParentTableViewDidLeaveFromTopNotification = @"HHParentTableViewDidLeaveFromTopNotification";
@interface HHCustomTalbeView : UITableView


@end
@implementation HHCustomTalbeView
//返回YES同时识别多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}


@end

@interface HHSixViewController ()<HHScrollPageViewDelegate,HHViewControllerDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) NSArray<NSString *> *titles;

@property(nonatomic, strong) HHScrollSegmentView *segmentView;

@property(nonatomic, strong) HHContentView *contentView;

@property(nonatomic, strong) UIView *headerView;

@property(nonatomic, strong) UIScrollView *childScrollView;

@property(nonatomic, strong) HHCustomTalbeView *tableView;


@end
static NSString *const cellID = @"cellID";
static CGFloat const segmentHeight = 44;
static CGFloat const headerViewHeight = 200;


@implementation HHSixViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    [self.view addSubview:self.tableView];
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            typeof (weakSelf) strongSelf = weakSelf;
            [strongSelf.tableView.mj_header endRefreshing];
        });

        
    }];
    
}
#pragma mark - <HHScrollPageViewDelegate>
-(NSInteger)numberOfChildViewControllers{

    return self.titles.count;
}

- (UIViewController<HHScrollPageViewChildDelegate> *)childViewController:(UIViewController<HHScrollPageViewChildDelegate> *)reuseViewController forIndex:(NSInteger)index{

    UIViewController <HHScrollPageViewChildDelegate>* childVc = reuseViewController;
    
    if (!childVc) {
        if (index %2 == 0) {
            
            childVc =[[HHTabController alloc]init];
            HHTabController *vc = (HHTabController *)childVc;
            vc.delegate = self;
        }else{
            childVc = [[HHCollectionViewController alloc]init];
            HHCollectionViewController *collectionView = (HHCollectionViewController *)childVc;
            collectionView.delegate =self;
            
        
        }
    }
    
    return childVc;
    
    
}

#pragma mark - <HHViewControllerDelegate>
-(void)scrollViewIsScrolling:(UIScrollView *)scrollView{
    _childScrollView = scrollView;
    
    NSLog(@"contentOffset%@",NSStringFromCGPoint(self.tableView.contentOffset));
    NSLog(@"yyyyyyyyyyyy---%f",self.tableView.contentOffset.y);
    
    if (self.tableView.contentOffset.y <headerViewHeight) {
        scrollView.contentOffset = CGPointZero;
        scrollView.showsVerticalScrollIndicator = NO;
    }else{
    
        self.tableView.contentOffset = CGPointMake(0, headerViewHeight);
        scrollView.showsVerticalScrollIndicator = YES;
        
    }
    
}
#pragma mark - <UIScrollViewDelegate>
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.childScrollView && _childScrollView.contentOffset.y>0) {
        self.tableView.contentOffset = CGPointMake(0, headerViewHeight);
    }
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY<headerViewHeight) {
        [[NSNotificationCenter defaultCenter]postNotificationName:HHParentTableViewDidLeaveFromTopNotification object:nil];
    }


}
#pragma mark - <UITableViewDataSource,UITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [cell.contentView addSubview: self.contentView];
    
    return cell;
    


}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    return self.segmentView;

}

-(HHScrollSegmentView *)segmentView{


    if (!_segmentView) {
        HHSegmentStyle *style =[[HHSegmentStyle alloc]init];
        style.showCover = YES;
        
        style.gradualChangeTitleColor = YES;
        style.coverBackgroundColor =[UIColor whiteColor];
        //标题一般状态颜色 --- 注意一定要使用RGB空间的颜色值
        style.normalTitleColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
        //标题选中状态颜色 --- 注意一定要使用RGB空间的颜色值
        style.selectedTitleColor = [UIColor colorWithRed:235.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:1.0];

        self.titles = @[@"新闻头条",
                        @"国际要闻",
                        @"体育",
                        @"中国足球",
                        @"汽车",
                        @"囧途旅游",
                        @"幽默搞笑",
                        @"视频",
                        @"无厘头",
                        @"美女图片",
                        @"今日房价",
                        @"头像",
                        ];
        
        __weak typeof(self) weakSelf = self;
        HHScrollSegmentView *segmentView =[[HHScrollSegmentView alloc]initWithFrame:CGRectMake(0, headerViewHeight, screen_width, segmentHeight) segmentStyle:style delegate:self titles:self.titles titleDidClick:^(HHTitleView *titleView, NSInteger index) {
           
            [weakSelf.contentView setContentOffSet:CGPointMake(screen_width*index, 0) animated:YES];
            
        }];
        segmentView.backgroundColor =[UIColor lightGrayColor];
        _segmentView = segmentView;
        
    }
    
    
    return _segmentView;
    

}
-(UIView *)headerView{

    if (!_headerView) {
        _headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_width, headerViewHeight)];
        UIImageView *imgView =[[UIImageView alloc]initWithFrame:self.headerView.bounds];
        imgView.image = [UIImage imageNamed:@"header"];
        [_headerView addSubview:imgView];
        
        
    }
    return _headerView;

}
-(HHContentView *)contentView{

    if (!_contentView) {
        HHContentView *contentView =[[HHContentView alloc]initWithFrame:self.view.bounds segmentView:self.segmentView parentViewController:self delegate:self];
        
        _contentView = contentView;
    }

    return _contentView;

}

-(HHCustomTalbeView *)tableView{


    if (!_tableView) {
        HHCustomTalbeView *tableView =[[HHCustomTalbeView alloc]initWithFrame:CGRectMake(0, 0, screen_width, screen_height) style:UITableViewStylePlain];
        tableView.tableHeaderView = self.headerView;
        tableView.tableFooterView = [UIView new];
        tableView.rowHeight = self.contentView.height;
        tableView.delegate = self;
        tableView.dataSource= self;
        tableView.sectionHeaderHeight = segmentHeight;
        tableView.showsHorizontalScrollIndicator = NO;
        
        _tableView = tableView;
    
    
    }
    return _tableView;

}

@end
